package com.centaline.androidsalesblog.base;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.view.View;

import com.android.volley.VolleyError;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.model.LatLng;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.mine.FootPrintActivity;
import com.centaline.androidsalesblog.activities.sale.SaleDetailActivity;
import com.centaline.androidsalesblog.api.sale.PostListApi;
import com.centaline.androidsalesblog.application.MyLocation;
import com.centaline.androidsalesblog.fragments.list.RentListFragment;
import com.centaline.androidsalesblog.iml.DropCompleteListener;
import com.centaline.androidsalesblog.iml.RefreshListener;
import com.centaline.androidsalesblog.model.businissobject.DropBo;
import com.centaline.androidsalesblog.model.responseobject.PostListRo;
import com.centaline.androidsalesblog.rx.map.RxMap;
import com.centaline.androidsalesblog.utils.DropUtil;
import com.centaline.androidsalesblog.widgets.dropdown.DropDownView;
import com.centanet.framework.interfaces.IResponseListener;
import com.trello.rxlifecycle.ActivityEvent;

import java.util.Locale;

import rx.functions.Action1;

/**
 * 租房基类
 */
public abstract class BaseRentListActivity extends BaseActivity {

    protected DropDownView dropDownView;
    protected RentListFragment rentListFragment;
    protected FloatingActionButton fab;

    protected PostListApi postListApi;
    private LocationClient locationClient;
    protected boolean needLocation;//是否需要定位
    protected RefreshListener refreshListener = new RefreshListener() {
        @Override
        public void down() {
            postListApi.setStartIndex(0);
            if (needLocation) {
                nearby();
            } else {
                request();
            }
        }

        @Override
        public void up(int count) {
            postListApi.setStartIndex(count);
            request();
        }
    };

    private DropCompleteListener dropCompleteListener = new DropCompleteListener() {
        @Override
        public void complete(int position, boolean fromMore, int type, DropBo... dropBos) {
            final int size = dropBos == null ? 0 : dropBos.length;
            if (fromMore) {
                //来源于更多
                if (size == 0) {
                    postListApi.setSort("0");
                    resetTab(3);
                    startRefresh(false);
                } else {
                    setTab(3, "更多");
                    postListApi.resetOther();
                    final StringBuilder feature = new StringBuilder();
                    for (DropBo dropBo : dropBos) {
                        final int innerType = dropBo.getType();
                        switch (innerType) {
                            case 100://排序
                                postListApi.setSort(dropBo.getValue());
                                break;
                            case 110://住宅类型
                                postListApi.setProperty(dropBo.getValue());
                                break;
                            case 60://面积
                                postListApi.setArea(dropBo.getValue());
                                break;
                            case 70://装修
                                postListApi.setFitment(dropBo.getValue());
                                break;
                            case 90://建造年代
                                postListApi.setConstruction(dropBo.getValue());
                                break;
                            case 50://朝向
                                postListApi.setDirection(dropBo.getValue());
                                break;
                            case 80:
                                feature.append(dropBo.getValue());
                                feature.append("_");
                                break;
                            default:
                                break;
                        }
                    }
                    if (feature.toString().contains("_")) {
                        feature.deleteCharAt(feature.length() - 1);
                    }
                    postListApi.setFeature(feature.toString());
                    startRefresh(false);
                }
            } else {
                switch (type) {
                    case 10://区域
                        postListApi.reset();
                        if (size == 1) {
                            if (DropUtil.compareValue(dropBos[0])) {
                                resetTab(position);
                            } else {
                                postListApi.setRegion(dropBos[0].getValue());
                                setTab(position, dropBos[0].getText());
                            }
                            startRefresh(false);
                        } else if (size == 2) {
                            postListApi.setRegion(dropBos[0].getValue());
                            if (DropUtil.compareValue(dropBos[1])) {
                                setTab(position, dropBos[0].getText());
                            } else {
                                postListApi.setScopeId(dropBos[1].getValue());
                                setTab(position, dropBos[1].getText());
                            }
                            startRefresh(false);
                        }
                        break;
                    case 11://附近
                        postListApi.reset();
                        if (size == 2) {
                            postListApi.setRound(dropBos[1].getValue());
                            startRefresh(true);
                        }
                        break;
                    case 20://地铁
                        postListApi.reset();
                        postListApi.setType("metro");
                        if (size == 1) {
                            if (DropUtil.compareValue(dropBos[0])) {
                                resetTab(position);
                            } else {
                                postListApi.setSubwayId(dropBos[0].getValue());
                                setTab(position, dropBos[0].getText());
                            }
                            startRefresh(false);
                        } else if (size == 2) {
                            postListApi.setSubwayId(dropBos[0].getValue());
                            if (DropUtil.compareValue(dropBos[1])) {
                                setTab(position, dropBos[0].getText());
                            } else {
                                postListApi.setStationId(dropBos[1].getValue());
                                setTab(position, dropBos[1].getText());
                            }
                            startRefresh(false);
                        }
                        break;
                    case 30://总价
                        postListApi.setPrice("");
                        postListApi.setMin("");
                        postListApi.setMax("");
                        if (size == 1) {
                            if (DropUtil.compareValue(dropBos[0])) {
                                resetTab(position);
                            } else {
                                postListApi.setPrice(dropBos[0].getValue());
                                setTab(position, dropBos[0].getText());
                            }
                            startRefresh(false);
                        } else if (size == 2) {
                            postListApi.setMin(dropBos[0].getValue());
                            postListApi.setMax(dropBos[1].getValue());
                            setTab(position, DropUtil.customPrice(dropBos[0], dropBos[1]));
                            startRefresh(false);
                        }
                        break;
                    case 40://房型
                        if (size == 0) {
                            postListApi.setRoom("");
                        } else {
                            postListApi.setRoom(dropBos[0].getValue());
                            if (DropUtil.compareValue(dropBos[0])) {
                                resetTab(position);
                            } else {
                                setTab(position, dropBos[0].getText());
                            }
                        }
                        startRefresh(false);
                        break;
                    case 60://面积
                        if (size == 0) {
                            postListApi.setArea("");
                        } else {
                            postListApi.setArea(dropBos[0].getValue());
                            if (DropUtil.compareValue(dropBos[0])) {
                                resetTab(position);
                            } else {
                                setTab(position, dropBos[0].getText());
                            }
                        }
                        startRefresh(false);
                        break;
                    case 130:
                        if (size == 1) {
                            postListApi.setEstCode(dropBos[0].getValue());
                        } else {
                            postListApi.setEstCode("");
                        }
                        startRefresh(false);
                        break;
                    default:
                        break;
                }
            }
        }
    };

    @Override
    protected int setLayoutId() {
        return R.layout.activity_rent_list;
    }

    @Override
    protected void findViews() {
        dropDownView = (DropDownView) findViewById(R.id.dropDownView);
        rentListFragment = (RentListFragment) getSupportFragmentManager().findFragmentById(R.id.rentFragment);
        fab = (FloatingActionButton) findViewById(R.id.fab);
    }

    @Override
    protected void initViews() {
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(BaseRentListActivity.this, FootPrintActivity.class));
            }
        });
        initDrop();

        dropDownView.setDropCompleteListener(dropCompleteListener);

        rentListFragment.setRefreshListener(refreshListener);

        postListApi = new PostListApi(SaleDetailActivity.class, PostListRo.class, new IResponseListener<PostListRo>() {
            @Override
            public void onResponse(PostListRo postListRo) {
                rentListFragment.stopRefresh(postListRo.getList());
                if (postListApi.getStartIndex() == 0) {
                    snack(postListRo.getTotal() == 0 ?
                            getString(R.string.snack_post_nothing) :
                            String.format(Locale.CHINA, getString(R.string.snack_post), postListRo.getTotal()));
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                rentListFragment.refreshError();
            }
        });
        postListApi.setPostType("r");

        locationClient = MyLocation.getLocationClient();
        RxMap.location(locationClient, false)
                .compose(this.<BDLocation>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Action1<BDLocation>() {
                    @Override
                    public void call(BDLocation bdLocation) {
                        locationInfo(bdLocation);
                    }
                });
    }

    /**
     * 初始化下拉菜单
     */
    protected abstract void initDrop();

    /**
     * 附近定位方法
     */
    protected void nearby() {
        locationClient.start();
    }

    /**
     * 开启下拉刷新
     */
    protected void startRefresh(boolean needLocation) {
        this.needLocation = needLocation;
        if (!needLocation) {
            rentListFragment.setLocation(null);
        }
        rentListFragment.startRefresh();
    }

    /**
     * 请求
     */
    protected void request() {
        postListApi.startRequest();
    }

    /**
     * 定位信息
     */
    protected void locationInfo(BDLocation bdLocation) {

    }

    /**
     * 重置标签
     *
     * @param position 位置
     */
    protected void resetTab(int position) {
        dropDownView.resetTab(position);
    }

    /**
     * 设置标签
     *
     * @param position 位置
     * @param tab      文本
     */
    protected void setTab(int position, String tab) {
        dropDownView.setTab(position, tab);
    }

    /**
     * 设置定位信息
     */
    protected void setLocation(LatLng location) {
        rentListFragment.setLocation(location);
    }
}
